package joctave.toolbox.io;

import joctave.core.tokens.*;
import joctave.core.tokens.numbertokens.DoubleNumberToken;
import joctave.core.functions.ExternalFunction;
import joctave.core.interpreter.GlobalValues;

import java.io.*;

public class fileparts extends ExternalFunction
{
	public OperandToken evaluate(Token[] operands, GlobalValues globals)
	{
        if (getNArgIn(operands) != 1)
			throwMathLibException("fileparts: number of arguments != 1");
        String file=operands[0].toString();
		int i = file.lastIndexOf('.');
		String ext="";
		if (i > 0) {
		    ext= file.substring(i+1);
		}
		String path=new File(file).getParent();
		String filename=file.substring(path.length()+1,file.length());
		i = filename.lastIndexOf('.');
		if (i > 0) {
		    filename=filename.substring(0,i);
		}
		
		//System.out.println("aptth"+path);
		//System.out.println("filenae"+filename);
		//System.out.println("extt"+ext);
        return new CharToken(new String[] {path,filename,ext});

	} // end eval
}

/*
@GROUP
IO
@SYNTAX
fileparts(String)
@DOC
displays the current working directory
@EXAMPLES
<programlisting>
fileparts("c:\dogma\dot.txt")
[C:\dogma ,dot,txt]
</programlisting>
@SEE
cd, createnewfile, dir, exist, mkdir, rmdir, delete, isfile, ishidden, lastmodified
*/

